/*
 * **********************************************************
 * File Name: Alarm.c
 * Description: ������������жϼ��߼�
 * 				ÿһλ����һ���������ͱ�־λ��
 * 				������������֧��16*8 =128������
 * 				������ٸ�����������ʵ��ʹ�ö���һ����󳤶�
 * Required File(s):	JD055ParallelRegister.h
 * 						OilPump.h
 * 						Motor.h
 * **********************************************************
 */

/*
 * **********************************************************
 * include files
 * **********************************************************
 */

#include "sys.h"
#include "stdio.h"
#include "Alarm.h"
#include "Timer.h"
#include "Settings.h"
#include "GetAnalog.h"
#include "Control.h"
#include "Protocol.h"
#include "ModbusRTU.h"
#include "ModbusRegister.h"
#include "EqualTemp.h"
#include "JD_InOut.h"
#include "AnalogInTrans.h"
#include "IDriver.h"

/*
 * **********************************************************
 * Private Defines
 * **********************************************************
 */
#define __ALARM_LOCK_VALUE__
#define __DEBUG_ALARM__
#define __MODBUS_RTU__

typedef struct
{
    ///����������Ӧ����ÿ�����������Ӧ������һ��
    u8 Direct;           //�����Ƚ��Ǵ��ڻ���С�ڣ�0��ʾ�Ǵ��ڵ��ڣ�1��ʾ��С�ڵ��ڣ����бȽ϶�������
    u8 ResetType;        //�ֶ���λ���Զ���λ���ǰ��Զ���λ
    u8 ActiveType;       //���ݲ�ͬ�������岻ͬ���ͣ���˵���Ǳ����͹��ϣ�������ͣ��������ͣ��
    u16 SourceCh;        //��ȡ����ͨ��.ͨ����2��ֵ���Զ�ȡ��ǰ����ֵ
    u16 LimitValueIndex; //����������������ֵ��ͨ�����ͺ�����ֵ����ȷ��Ψһ����
    u16 TimeValueIndex;  //���ʱ�������������ֵ��ͨ�����ͺ�����ֵ���Ի��Ψһ��ֵ
    u16 RefStatusIndex;  //��������Ǹ���ȫ��״̬������bitֵ���б����Ƿ������ж�
} stAlarmParam;
//������Ҫ���ݸ������״̬��ϵͳ״̬������һ���Լ���Ҫ��״̬���������״̬���趨��Ҫ��״̬�����б����Ƿ���

///����ÿ�ֱ������������ʱ��ͬ�㲻ͬ��������������Ƶ�ʱ��ͽ�������
///����ͬһ�������������ò�ͬʱ�䣬����һ������
///ͬ������������ż����ڲ�ͬ״̬�в�ֵͬ����ôҲ������ͬ����

#define ALERT_CH_DISABLE 32000
#define ALERT_DELAY_POLL_TIME 10

#define ALERT_CHECK_DISABLE 0
#define ALERT_CHECK_ABLE 1

#define ALERT_COMPARE_GREAT 0
#define ALERT_COMPARE_LOW 1

#define ALERT_RESET_MANUAL 0
#define ALERT_RESET_AUTO 1
#define ALERT_RESET_CONDENSER_TEMP 2
#define ALERT_RESET_LSA 3

#define ALERT_DELAY_AI_TEMP_TRIP 400
#define ALERT_DELAY_DI_TRIP 411
#define ALERT_DELAY_DI_DOOR_OPEN HREG_DOOR_OPEN_WARN_DELAY
#define ALERT_DELAY_TEMP_LOW HREG_LOW_TEMP_ALARM_DELAY
#define ALERT_DELAY_TEMP_HIGH HREG_HIGH_TEMP_ALARM_DELAY

#define ALERT_ACTIVE_STOP 0
#define ALERT_ACTIVE_NONE 1

#define ALERT_TRIGGER_NONE 0
#define ALERT_TRIGGER_HAVE 1

/*
 * Check States
 */
#define ALERT_VALID_ALL (0xffff)
#define ALERT_VALID_SET (0x0001)
#define ALERT_VALID_NONE 0
#define ALERT_VALID_HIGH (1 << 1) 
#define ALERT_VALID_LOW (1 << 2) 
#define ALERT_VALID_DO (1 << 3) 

/*
 * ȡ�ñȽ�ֵ����
 */

#define ALERT_SET_VALUE_COMM_TRIP_HIGH 20

#define ALERT_SET_VALUE_DI_TRIP_LOW 400
#define ALERT_SET_VALUE_DI_TRIP_HIGH 401
#define ALERT_SET_VALUE_AI_FAULT_HIGH 402
#define ALERT_SET_VALUE_AI_SHORT_FAULT_HIGH 403
#define ALERT_SET_VALUE_TEMP_LOW 404
#define ALERT_SET_VALUE_TEMP_HIGH 405
#define ALERT_SET_VALUE_CONDENSER_TEMP_HIGH 406
#define ALERT_SET_VALUE_SUC_TEMP_LOW 407
#define ALERT_SET_VALUE_BATTERY_LOW_VALUE 501
#define ALERT_SET_VALUE_COM_FAIL_VALUE 502
//ͨѶ��ַ
#define ALERT_SET_VALUE_SUC_HEAT_LOW HREG_LOWSH_SUPER_HEAT
#define ALERT_SET_VALUE_EVAPORATOR_PRESS_HIGH HREG_MOP_TEMP
#define ALERT_SET_VALUE_EVAPORATOR_TEMP_LOW HREG_LOP_TEMP
/*
 * **********************************************************
 * Private Variables
 * **********************************************************
 */

const u16 AlarmDelayExtraArray[12] =
    {
        5, //	ALERT_DELAY_AI_TEMP_ALARM	400
        2,  //	ALERT_DELAY_AI_PRESS_ALARM	401
        50, //	ALERT_DELAY_AI_TEMP_TRIP	402
        2,  //	ALERT_DELAY_AI_PRESS_TRIP	403
        10, //	ALERT_DELAY_MOTOR_CURR_ALARM	404
        20, //	ALERT_DELAY_CAP_VOLT_ALARM	405
        50, //	ALERT_DELAY_DISCHARGEIOLPRESS_DIFF	406
        0,  //	ALERT_DELAY_GENERAL	407
        50, //	ALERT_DELAY_SUCT_PRESS	408
        50, //	ALERT_DELAY_MOTOR_CURR_LOW_TRIP	409
        20, //	ALERT_DELAY_FEEDBACK_DI_RUN	410
        20, //	ALERT_DELAY_DI_TRIP411
};

stAlarmService AlarmService;
//{
//.SetPollDelay = 5000,
//.Ready = 1,
//};

stAlarmInput AlarmInput[MAX_ALARM_NO];

const stAlarmParam AlarmParam[MAX_ALARM_NO] =
{
	{ALERT_COMPARE_GREAT, ALERT_RESET_AUTO, ALERT_ACTIVE_NONE, ALERT_SENSOR1, ALERT_SET_VALUE_AI_FAULT_HIGH, ALERT_DELAY_AI_TEMP_TRIP, ALERT_VALID_SET},                  ///	20
	{ALERT_COMPARE_GREAT, ALERT_RESET_AUTO, ALERT_ACTIVE_NONE, ALERT_SENSOR2, ALERT_SET_VALUE_AI_FAULT_HIGH, ALERT_DELAY_AI_TEMP_TRIP, ALERT_VALID_SET},                  /// 20
	{ALERT_COMPARE_GREAT, ALERT_RESET_AUTO, ALERT_ACTIVE_NONE, ALERT_SENSOR3, ALERT_SET_VALUE_AI_FAULT_HIGH, ALERT_DELAY_AI_TEMP_TRIP, ALERT_VALID_SET},                  /// 20
	{ALERT_COMPARE_GREAT, ALERT_RESET_AUTO, ALERT_ACTIVE_NONE, ALERT_SENSOR4, ALERT_SET_VALUE_AI_FAULT_HIGH, ALERT_DELAY_AI_TEMP_TRIP, ALERT_VALID_SET},                  /// 20
	{ALERT_COMPARE_GREAT, ALERT_RESET_AUTO, ALERT_ACTIVE_NONE, ALERT_SHORT_TEMP_SENSOR1, ALERT_SET_VALUE_AI_SHORT_FAULT_HIGH, ALERT_DELAY_AI_TEMP_TRIP, ALERT_VALID_SET}, ///	20
	{ALERT_COMPARE_GREAT, ALERT_RESET_AUTO, ALERT_ACTIVE_NONE, ALERT_SHORT_TEMP_SENSOR2, ALERT_SET_VALUE_AI_SHORT_FAULT_HIGH, ALERT_DELAY_AI_TEMP_TRIP, ALERT_VALID_SET}, /// 20
	{ALERT_COMPARE_GREAT, ALERT_RESET_AUTO, ALERT_ACTIVE_NONE, ALERT_SHORT_TEMP_SENSOR3, ALERT_SET_VALUE_AI_SHORT_FAULT_HIGH, ALERT_DELAY_AI_TEMP_TRIP, ALERT_VALID_SET}, /// 20
	{ALERT_COMPARE_GREAT, ALERT_RESET_AUTO, ALERT_ACTIVE_NONE, ALERT_SHORT_TEMP_SENSOR4, ALERT_SET_VALUE_AI_SHORT_FAULT_HIGH, ALERT_DELAY_AI_TEMP_TRIP, ALERT_VALID_SET}, /// 20
	{ALERT_COMPARE_GREAT, ALERT_RESET_AUTO, ALERT_ACTIVE_NONE, ALERT_HIGH_TEMP,ALERT_SET_VALUE_TEMP_HIGH, ALERT_DELAY_TEMP_HIGH, ALERT_VALID_HIGH},                       /// 6           ZLC 180529
	{ALERT_COMPARE_LOW,  ALERT_RESET_AUTO,  ALERT_ACTIVE_NONE, ALERT_LOW_TEMP,ALERT_SET_VALUE_TEMP_LOW, ALERT_DELAY_TEMP_LOW, ALERT_VALID_LOW},                           /// 5
	{ALERT_COMPARE_GREAT, ALERT_RESET_AUTO, ALERT_ACTIVE_NONE, ALERT_CONDENSER_HIGH_TEMP, ALERT_SET_VALUE_CONDENSER_TEMP_HIGH, ALERT_DELAY_AI_TEMP_TRIP, ALERT_VALID_ALL},/// 20
	{ALERT_COMPARE_GREAT, ALERT_RESET_AUTO, ALERT_ACTIVE_NONE, ALERT_DOOR_OPEN,ALERT_SET_VALUE_DI_TRIP_HIGH, ALERT_DELAY_DI_DOOR_OPEN, ALERT_VALID_DO},                   ///	15

};

/*
 * **********************************************************
 * Private Functions
 * **********************************************************
 */

u16 Alert_Get_DI_Status(void)
{
    u16 temp = 0;

    return temp;
}

u16 Alert_Get_Status(u16 ch)
{

    return ' ';
}

s16 Alert_Get_Value(u16 ch)
{
    s16 value = 0xffff;
    //u16 temp = 0;
    switch (ch)
    {
    case ALERT_SENSOR1:
    case ALERT_SHORT_TEMP_SENSOR1:
        value = IGet_AI_Scaled_Value(0);
        break;
    case ALERT_SENSOR2:
    case ALERT_SHORT_TEMP_SENSOR2:
        value = IGet_AI_Scaled_Value(1);
        break;

    case ALERT_SENSOR3:
    case ALERT_SHORT_TEMP_SENSOR3:
        value = IGet_AI_Scaled_Value(2);

        break;
    case ALERT_SENSOR4:
    case ALERT_SHORT_TEMP_SENSOR4:
        value = IGet_AI_Scaled_Value(3);

        break;
    case ALERT_HIGH_TEMP:
        value = GetWarnTempValue();
        if (value > 32000)
        {
            value = 32000;
        }
        break;
    case ALERT_LOW_TEMP:
        value = GetWarnTempValue();
        if (value > 32000)
        {
            value = 32000;
        }
        break;
    case ALERT_DOOR_OPEN:
        value = GetDoorOpenValue();
        break;

    case ALERT_CONDENSER_HIGH_TEMP:
        value = GeCcondensertTempValue();
        if (value > 32000)
        {
            value = 32000;
        }

        break;
    default:
        break;
    }
    return value;
}

s16 Alert_Get_LimitValue(u16 Index)
{
    if (Index == ALERT_SET_VALUE_DI_TRIP_LOW)
    {
        return 0;
    }
    else if (Index == ALERT_SET_VALUE_DI_TRIP_HIGH)
    {
        return 1;
    }
    else if (Index == ALERT_SET_VALUE_AI_FAULT_HIGH)
    {
        return CONVERT_OVER_RANGE_FAIL;
    }
    else if (Index == ALERT_SET_VALUE_AI_SHORT_FAULT_HIGH)
    {
        return CONVERT_UNDER_RANGE_FAIL;
    }
    else if (Index == ALERT_SET_VALUE_TEMP_LOW)
    {
        return GetJDParamValue(HREG_LOW_ALARM_TEMP);
    }
    else if (Index == ALERT_SET_VALUE_TEMP_HIGH)
    {
        return GetJDParamValue(HREG_HIGH_ALARM_TEMP);
    }
    else if (Index == ALERT_SET_VALUE_CONDENSER_TEMP_HIGH)
    {
        if (ALARM_ACTIVE_NORMAL == AlarmInput[ALERT_CONDENSER_HIGH_TEMP].Alarm)
        {
            return GetJDParamValue(HREG_CONDERSER_ALARM_TEMP);
        }
        else
        {
            return GetJDParamValue(HREG_CONDERSER_RELIEVE_ALARM_TEMP);
        }
    }

    else
    {
        return GetJDParamValue(Index);
    }
}

u32 Alert_Get_LimitTime(u16 Index)
{
    if (Index >= 400)
    {
        return AlarmDelayExtraArray[Index - 400]  * 25;
    }
    else if (Index == ALERT_DELAY_TEMP_HIGH)
    {
				return 60 * GetJDParamValue(HREG_HIGH_TEMP_ALARM_DELAY) * 25;
			
    }
    else if (ALERT_DELAY_TEMP_LOW == Index)
    {

        return 60 * GetJDParamValue(HREG_LOW_TEMP_ALARM_DELAY) *25;
    }
    else if (Index == ALERT_DELAY_DI_DOOR_OPEN)
    {

        return 60 * GetJDParamValue(HREG_DOOR_OPEN_WARN_DELAY) *25;
    }
    else
    {
        return GetJDParamValue(Index) * 25;
    }
		
}
void Clear_TempTrip(void)
{
    u8 i;
    Alert_Set_Reset();
    for (i = ALERT_HIGH_TEMP; i <= ALERT_CONDENSER_HIGH_TEMP; i++)
    {
        AlarmInput[i].CurState = 0;
        AlarmInput[i].Alarm = ALARM_INACTIVE;
    }
}

void Clear_SystemTrip(void)
{
    u8 i;
    Alert_Set_Reset();
    for (i = 0; i < MAX_ALARM_NO; i++)
    {

        AlarmInput[i].CurState = 0;
        AlarmInput[i].Alarm = ALARM_INACTIVE;
    }
}

void Alert_Set_Reset(void)
{
    AlarmService.TripCount = 0;
}

///����������ݵ�ǰֵ���趨ֵ�ͱȽϷ��򣬷���һ���ȽϽ��
///�ڲ��Ƚϲ��ð������ڵķ�ʽ�����������������1�����������������򷵻�0������û�д���

u8 Alert_Compare_Value(s16 Value, s16 LimitValue, u8 direct, u16 ch)
{
    u8 ttt;
    if (direct == ALERT_COMPARE_GREAT)
    {
        if (ALERT_SHORT_TEMP_SENSOR4 >= ch)
        {
            if (Value == LimitValue)
            {
                ttt = ALERT_TRIGGER_HAVE;
            }
            else
            {
                ttt = ALERT_TRIGGER_NONE;
            }
        }
        else if (Value >= LimitValue)
        {
            ttt = ALERT_TRIGGER_HAVE;
        }
        else
        {
            ttt = ALERT_TRIGGER_NONE;
        }
    }
    else
    {
        if (Value < LimitValue)
        {
            ttt = ALERT_TRIGGER_HAVE;
        }
        else
        {
            ttt = ALERT_TRIGGER_NONE;
        }
    }
    return ttt;
}

u8 Get_Alert_Indication(void)
{

    u8 i;
    u8 temp = 0;
    for (i = 0; i < MAX_ALARM_NO; i++)
    {

        if (AlarmInput[i].Alarm == ALARM_ACTIVE_TRIP)
        {
            temp = 1;
            break;
        }
    }
    return temp;
}

u8 Get_Alert_State(u8 AlarmNo)
{
    return AlarmInput[AlarmNo].Alarm;
}

void Alert_Active(void)
{
    //AlarmService.TripCount = 0;
    //u8 i = 0;

    //for (i = 0; i < MAX_ALARM_NO; i++)
    //{
    //if (AlarmInput[i].Alarm == ALARM_ACTIVE_TRIP)
    //{
    //  AlarmService.TripCount++;
    //}
    //}
}

/*
 * **********************************************************
 * Public Functions
 * **********************************************************
 */

/*
 * Function Name:	Alarm_Get_State
 * Description:	��������ѯ������״ֵ̬
 * Return Value:	��������ѯ�ı���״̬
 * Parameter:	index	alarm.h�к궨���ֵ
 * Note:
 */
u8 Alarm_Get_State(u16 index)
{
    if ((AlarmInput[index].Alarm == ALARM_ACTIVE_TRIP))
    {
        return 1;
    }
    else
    {
        return 0;
    }
}

/*
 * Function Name:	Alarm_Get_StateChar
 * Description:	��������ѯ�����ĵ�ǰ״̬�ַ�
 * Return Value:	��������ѯ�ı���״̬�ַ���' ''S''T''H''L''F'
 * Parameter:	channel	ͨ����ź궨��
 * Note:
 */
u8 Alarm_Get_StateChar(u8 channel)
{

    return ' ';
}

/*
 * Function Name:	Alarm_Get_DisplayValue
 * Description:	����ͨ��ֵ������ʱ����
 * Return Value:	s16	����16λ�з�����
 * Parameter:	channel	ͨ����ź궨��
 * Note:
 */
s16 Alarm_Get_DisplayValue(u8 channel)
{

    return 0;
}

/*
 * Function Name:	Alarm_Reset_All
 * Description:	��λ����
 * Return Value:	��
 * Parameter:	��
 * Note:
 */
void Alarm_Reset_All(void)
{
    Clear_SystemTrip();
	  Controller.WaitDelay = 2;
}

void Alarm_Reset_EXCEPT_DOOR_OPEN(void)
{
    u8 i;
    Alert_Set_Reset();
    for (i = 0; i < ALERT_DOOR_OPEN; i++)
    {

        AlarmInput[i].CurState = 0;
        AlarmInput[i].Alarm = ALARM_INACTIVE;
    }

//    for (i = (ALERT_DOOR_OPEN + 1); i < MAX_ALARM_NO; i++)
//    {

//        AlarmInput[i].CurState = 0;
//        AlarmInput[i].Alarm = ALARM_INACTIVE;
//    }
}

//�����������һ����һ������ֵ�����λ���ۺ�ʱ��������Ϊ1������λ���ݶ���ȷ��
//�����ں�0xff���бȽ�ʱ����֤���Ϊ��0ֵ
u16 Alert_Get_RefStatus(u8 index)
{
    //�������һ������ֵ���������κ�״̬�����б������ı�����Ч
    //������������ε�����ô���Ϊ0��Ҳ�������м��
    u16 result = FALSE;
    u8 SensorChnnel = 0;

    switch (AlarmParam[index].RefStatusIndex)
    {

    case ALERT_VALID_ALL:
        result = TRUE;
        break;

    case ALERT_VALID_SET:
        SensorChnnel = AlarmParam[index].SourceCh ;

        if (((11 == GetJDParamValue(HREG_CONTROL_CALI))&&(SensorChnnel ==0))
					  ||((11 == GetJDParamValue(HREG_DEF_CALI))&&(SensorChnnel ==2))
			      	||((11 == GetJDParamValue(HREG_CONDENSER_CALI))&&(SensorChnnel ==3)))
        {
            result = FALSE;
        }
        else
        {
            result = TRUE;
        }
        break;

    case ALERT_VALID_HIGH:
       if ((251 == GetJDParamValue(HREG_HIGH_TEMP_ALARM_DELAY))
				|| (Controller.PwrOnRhAlarmDelay > 0)
				    || (DEF_ON_DELAY == Controller.Defrost.Progress) 
			         || (DRIP_DELAY == Controller.Defrost.Progress)
			            || (DRIP == Controller.Defrost.Progress)
			               || (Controller.Defrost.DripAlarmDelay >0))
        {
            result = FALSE;
        }
        else
        {
            result = TRUE;
        }
        break;		
		case	ALERT_VALID_LOW:
			 if (251 == GetJDParamValue(HREG_LOW_TEMP_ALARM_DELAY))
        {
            result = FALSE;
        }
        else
        {
            result = TRUE;
        }
        break;		
		case ALERT_VALID_DO:			
			  if (1 == GetJDParamValue(HREG_DI1_SET))
        {
            result = TRUE;
        }
        else
        {
            result = FALSE;
        }
        break;
    default:
        break;
    }

    return result;
}

/*
 * Function Name:	Get_AI_Alarm_Status
 * Description:	ģ��������״̬
 * Return Value:	��λȡ��־
 * Parameter:	��
 * Note:	��ģ��������λ��ͬ
 */
u16 Get_AI_Alarm_Status(void)
{

    u16 result = 0;

    return result;
}

/*
 * Function Name:	Trip_Digitals_State
 * Description:	����������״̬
 * Return Value:	��λȡ��־
 * Parameter:	��
 * Note:	�뿪��������λ��ͬ
 */
u16 Trip_Digitals_State(void)
{
    u16 result = 0;
    return result;
}
/*
 * **********************************************************
 * Public Service
 * **********************************************************
 */

///��ʱ�߼��Ĵ���ԭ��
///0������StateChg����ȷ��״̬�Ƿ�ı䣬����ı䣬�����Ǵ��޵���״̬�����趨��ʼ��־������ֵ
///1���ڶ�ʱ������־��ֵ�Ͷ�ʱʱ�丳ֵ��chg��־�����ȷ���´β��ᱻ���¸�ֵ
///2�����������Ϊ0�������и�ֵ�������������Ϊ0���������������򸳳�ֵ
///3����������Ϊ�㣬����������־Ϊ1�ǣ����������ڼ���ֱ��Ϊ0
///4����������Ϊ0�ǣ�����

/*
 * Function Name:	Alarm_Poll
 * Description:	�����ж������̣������Ӧ�ó��򲿷�������
 * Return Value:	��
 * Parameter:	��
 * Note:
 */
u8 Alarm_Poll(void)
{
    static u8 iii;
    u8 LastState; //���������־2��״̬�Ƿ�һ�£������һ�����¸�ֵʱ��
    u8 AlertLastStatus;
    //100msִ��һ��
    u16 Now;
    static u16 timediff;
    s16 Value;
    s16 ValueLimit;
    u32 TimeLimit;

    stAlarmParam const *pAlarmParam;
    stAlarmInput *pAlarmInput = NULL;

    //s16 ResetType;
#ifdef __ALARM_LOCK_VALUE__
    //u8	lockMark;
#endif
    //�й��ϲ����
    if (Controller.AlarmStartDelay > 0)
    {
        return 0;
    }

    Now = Timer2_Get16Ticks();
    timediff = Now - AlarmService.LastAlarmTicks;
    if (timediff < AlarmService.SetPollDelay)
    {
        if (AlarmService.Ready)
        {
            for (iii = 0; iii < MAX_ALARM_NO; iii++)
            {
                //AlarmInput[iii].LockValue = Alert_Get_Value(AlarmParam[iii].SourceCh);	//����ǰֵ
            }
        }
        return (u8)0;
    }
    else
    {
        AlarmService.LastAlarmTicks = Now;
        if (timediff > ALERT_DELAY_POLL_TIME)
        {
            timediff %= ALERT_DELAY_POLL_TIME;
            AlarmService.SetPollDelay = ALERT_DELAY_POLL_TIME - timediff;
        }
        else
        {
            AlarmService.SetPollDelay = ALERT_DELAY_POLL_TIME;
        }
    }

    //	AlarmService.Ready = 0;

    do
    {
        if( Controller.ManualCommand >= COOL_FAN_OFF)
        {
            if (DOOR_OFF == GetDoorOpenValue())
            {
                Alarm_Reset_All();
                return 0;
            }
            else if (DOOR_ON == GetDoorOpenValue())
            {
                Alarm_Reset_EXCEPT_DOOR_OPEN();
            }
        }
#ifdef __DEBUG_ALARM__
        if (iii == AlarmService.ttt)
        {
            __asm("nop");
        }
#endif
        pAlarmParam = &AlarmParam[iii];
        pAlarmInput = &AlarmInput[iii];

        Value = Alert_Get_Value(pAlarmParam->SourceCh); //����ǰֵ

        {
            //����Ҫ����״̬����ֱ�ӵ���һ��ͨ��
            AlertLastStatus = pAlarmInput->CurStatus;
            pAlarmInput->CurStatus = (FALSE == Alert_Get_RefStatus(pAlarmParam->SourceCh)) 
					    || (/*pAlarmInput->*/ Value == ALERT_CH_DISABLE);

            //ResetType = Alert_Get_ResetType(pAlarmParam->ResetType);
            if (pAlarmInput->Alarm == ALARM_ACTIVE_TRIP && pAlarmParam->ResetType == ALERT_RESET_MANUAL)
            { //	�ֶ���λ�Ĳ��ټ�⣬�ȴ���λ����
                __asm("nop");
            }
            else if (pAlarmInput->CurStatus)
            {
                pAlarmInput->Alarm = ALARM_INACTIVE;
            }
            else //����״̬�����д���
            {
                ValueLimit = Alert_Get_LimitValue(pAlarmParam->LimitValueIndex); //���趨ֵ
                TimeLimit = Alert_Get_LimitTime(pAlarmParam->TimeValueIndex);    //���趨ʱ��
                LastState = pAlarmInput->CurState;                               //�����ϴ�״̬

                pAlarmInput->CurState = Alert_Compare_Value(Value, ValueLimit, pAlarmParam->Direct, pAlarmParam->SourceCh);
                if (pAlarmInput->CurState) //��ǰ�д���״̬
                {
                    if ((LastState != pAlarmInput->CurState)            //2�β���ȣ�˵���ǵ�һ�Σ�Ҫ���¸�ֵ
                        || (AlertLastStatus != pAlarmInput->CurStatus)) //2��ʹ�ܲ�һ����Ҫ���¸�ֵ
                    {
                        pAlarmInput->Time = TimeLimit + 1;
#ifdef __DEBUG_ALARM__
                        if (iii == AlarmService.ttt)
                        {
                            __asm("nop");
                        }
#endif
                    }
                    if (pAlarmInput->Time)
                    {
#ifdef __DEBUG_ALARM__
                        if (iii == AlarmService.ttt)
                        {
                            __asm("nop");
                        }
#endif
                        pAlarmInput->Time--;
                        if (!pAlarmInput->Time)
                        {
                            pAlarmInput->Alarm = ALARM_ACTIVE_TRIP;
                            //pAlarmInput->Identifier = pAlarmParam->SetIdentifier;
#ifdef __DEBUG_ALARM__
                            if (iii == AlarmService.ttt)
                            {
                                __asm("nop");
                            }
#endif
                        }
                        else
                        {
                            pAlarmInput->Alarm = ALARM_ACTIVE_WAIT;
                        }
                    }
                    else
                    {
                        pAlarmInput->Alarm = ALARM_ACTIVE_TRIP;
                        //pAlarmInput->Identifier = pAlarmParam->SetIdentifier;
                    }
                }
                else //��ǰ�޴���״̬
                {
                    pAlarmInput->Alarm = ALARM_ACTIVE_NORMAL;
                    //pAlarmInput->Identifier = ' ';
#ifdef __DEBUG_ALARM__
                    if (iii == AlarmService.ttt)
                    {
                        __asm("nop");
                    }
#endif
                }
            } //end active
        }

        iii++;
        if (iii > MAX_ALARM_NO - 1)
        {
            iii = 0;
            break;
        }
    } while (iii % 3);
    if (!iii)
    {

        Alert_Active();
    }

    return (u8)0;
}

/*
 * **********************************************************
 * Initial Function
 * **********************************************************
 */

/*
 * Function Name:	Alamr_Initial
 * Description:	������ʼ�������������Ӧ�ó��򲿷ֳ�ʼ���У��ڲ�����ʼ����
 * Return Value:	��
 * Parameter:	��
 * Note:
 */
u8 Alamr_Initial(void)
{

    return (u8)0;
}

u8 GetDoorOpenValue()
{
    u8 DoorValue = DOOR_OFF;
  
    if ((( 0 == IGet_DI_State(DI_DOOR)))&&(1 == GetJDParamValue(HREG_DI1_SET))) //��i�����ֵ�������
    {
       DoorValue = DOOR_ON;

    }
    return DoorValue;
}

s16 GetWarnTempValue()
{
    s16 Temp ;
	
    Temp = GetGoodsTemp();
	  if(Temp>=3200)
	  {
		  Temp =32000;
	  }
    return Temp;
}


s16 GeCcondensertTempValue()
{
    s16 Temp ;
	
    Temp = GetCondenserTemp();
	  if(Temp>=3200)
	  {
		  Temp =32000;
	  }
    return Temp;
}

/*
 * Function Name:	IGet_Warn_States1
 * Description:	�����ź�״̬
 * Return Value:
 * Parameter:
 *
 * Note:
 */

u16 IGet_Warn_States()
{
    u16 WarnState = 0;

    if (ALARM_ACTIVE_TRIP == AlarmInput[ALERT_SENSOR1].Alarm)
    {
        WarnState |= 1 << 0;
    }

    if (ALARM_ACTIVE_TRIP == AlarmInput[ALERT_SENSOR2].Alarm)
    {
        WarnState |= 1 << 1;
    }

    if (ALARM_ACTIVE_TRIP == AlarmInput[ALERT_SENSOR3].Alarm)
    {
        WarnState |= 1 << 2;
    }

    if (ALARM_ACTIVE_TRIP == AlarmInput[ALERT_SENSOR4].Alarm)
    {
        WarnState |= 1 << 3;
    }
    if (ALARM_ACTIVE_TRIP == AlarmInput[ALERT_SHORT_TEMP_SENSOR1].Alarm)
    {
        WarnState |= 1 << 4;
    }

    if (ALARM_ACTIVE_TRIP == AlarmInput[ALERT_SHORT_TEMP_SENSOR2].Alarm)
    {
        WarnState |= 1 << 5;
    }

    if (ALARM_ACTIVE_TRIP == AlarmInput[ALERT_SHORT_TEMP_SENSOR3].Alarm)
    {
        WarnState |= 1 << 6;
    }

    if (ALARM_ACTIVE_TRIP == AlarmInput[ALERT_SHORT_TEMP_SENSOR4].Alarm)
    {
        WarnState |= 1 << 7;
    }

    if (ALARM_ACTIVE_TRIP == AlarmInput[ALERT_HIGH_TEMP].Alarm)
    {
        WarnState |= 1 << 8;
    }
    if (ALARM_ACTIVE_TRIP == AlarmInput[ALERT_LOW_TEMP].Alarm)
    {
        WarnState |= 1 << 9;
    }
    if (ALARM_ACTIVE_TRIP == AlarmInput[ALERT_DOOR_OPEN].Alarm)
    {
        WarnState |= 1 << 10;
    }
    if (ALARM_ACTIVE_TRIP == AlarmInput[ALERT_CONDENSER_HIGH_TEMP].Alarm)
    {
        WarnState |= 1 << 11;
    }
    return WarnState;
}


u8 GetControlSensorFault()
{
    u8 SensorFault = 0;

      if ((ALARM_ACTIVE_TRIP == AlarmInput[ALERT_SENSOR1].Alarm)
           || (ALARM_ACTIVE_TRIP == AlarmInput[ALERT_SHORT_TEMP_SENSOR1].Alarm))
        {
             SensorFault = SENSOR_FAULT;
        }
    return SensorFault;
}
